﻿import request from '/@/utils/request';
import other from '/@/utils/other';
import { AxiosResponse } from 'axios';
import { ISysPerson, ISysPersonView, ISysPersonVm } from '/@/views/person/sysperson/api/interface';

interface ISysPersonApi {
  search(data: object): Promise<AxiosResponse<ISysPersonView[]>>;

  get(id: string | number): Promise<AxiosResponse<ISysPersonVm>>;

  add(data: ISysPersonVm): Promise<AxiosResponse<ISysPerson>>;

  edit(data: ISysPersonVm): Promise<AxiosResponse<ISysPerson>>;

  batchEdit(data: object): Promise<AxiosResponse>;

  delete(ids: (string | number)[]): Promise<AxiosResponse<number>>;

  export(data: object): Promise<void>;

  exportById(ids: (string | number)[]): Promise<void>;

  import(data: object): Promise<AxiosResponse<number>>;

  approve(data: object, tag: string): Promise<AxiosResponse<any, any>>;

  createAccounts(ids: (string | number)[]): Promise<AxiosResponse<number>>;

  genSuppByIds(ids: (string | number)[]): Promise<AxiosResponse<number>>;

  genSuppBySearch(data: object): Promise<AxiosResponse<number>>;

}

function SysPersonApi(): ISysPersonApi {
  return {
    async search(data: object): Promise<AxiosResponse<ISysPersonView[]>> {
      return await request({
        url: '/api/SysPerson/Search',
        method: 'post',
        data,
      });
    },
    async get(id: string | number): Promise<AxiosResponse<ISysPersonVm>> {
      return await request({
        url: `/api/SysPerson/${id}`,
        method: 'get',
      });
    },
    async add(data: ISysPersonVm): Promise<AxiosResponse<ISysPerson>> {
      return await request({
        url: '/api/SysPerson/Add',
        method: 'post',
        data,
      });
    },
    async edit(data: ISysPersonVm): Promise<AxiosResponse<ISysPerson>> {
      return await request({
        url: '/api/SysPerson/Edit',
        method: 'put',
        data,
      });
    },
    async batchEdit(data: object): Promise<AxiosResponse> {
      return await request({
        url: '/api/SysPerson/batchEdit',
        method: 'post',
        data,
      });
    },
    async delete(ids: (string | number)[]): Promise<AxiosResponse<number>> {
      return await request({
        url: '/api/SysPerson/BatchDelete',
        method: 'post',
        data: ids,
      });
    },
    async export(data: object): Promise<void> {
      const response = await request<any, Blob>({
        responseType: 'blob',
        url: '/api/SysPerson/ExportExcel',
        method: 'post',
        data,
      });
      other.downloadFile(response);
    },
    async exportById(ids: (string | number)[]): Promise<void> {
      const response = await request<any, Blob>({
        responseType: 'blob',
        url: '/api/SysPerson/ExportExcelByIds',
        method: 'post',
        data: ids,
      });
      other.downloadFile(response);
    },
    async import(data: object): Promise<AxiosResponse<number>> {
      return await request({
        url: '/api/SysPerson/Import',
        method: 'post',
        data,
      });
    },
    async approve(data: object, tag: string) {
      return request({
        url: '/api/SysPerson/approve?tag=' + tag,
        method: 'post',
        data,
      });
    },
    // 注册
    async createAccounts(ids: (string | number)[]): Promise<AxiosResponse<number>> {
      return await request({
        url: '/api/SysPerson/CreateAccounts',
        method: 'post',
        data: ids,
      });
    },
    // 通过勾选ID一键生成人员补录数据
    async genSuppByIds(ids: (string | number)[]): Promise<AxiosResponse<number>> {
      return await request({
        url: '/api/SysPerson/GenSuppByIds',
        method: 'post',
        data: ids,
      });
    },
    // 通过查询条件一键生成人员补录数据
    async genSuppBySearch(data: object): Promise<AxiosResponse<number>> {
      return await request({
        url: '/api/SysPerson/GenSuppBySearch',
        method: 'post',
        data,
      });
    },
  };
}

export { SysPersonApi };
